/**
 * 
 */
package com.java.controller.front;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.java.entity.Users;
import com.java.service.impl.UserServiceImpl;
import com.java.utils.CookieConstantTable;
import com.java.utils.CookieUtils;

/**
* @author Kidney
* 创建时间：2019年4月24日 上午1:45:16
* Description:
*/
/**
 * @author KIDNEY
 *
 */
@Controller
public class LoginController {

	@Autowired
	private UserServiceImpl us;
	
	@RequestMapping("logout")
	public String logout(Model model,HttpServletRequest request,HttpServletResponse response) {
		CookieUtils.delCookie( response, CookieUtils.getCookieValue(request, "username"));
		request.getSession().removeAttribute("user");
		request.setAttribute("msg", "已退出");
		System.out.println("username");
		return "index.jsp";
	}

	@RequestMapping("login")
	public String login(Model model, String username, String password, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		// Session中存好的验证码数值
		String piccode = (String) request.getSession().getAttribute("piccode");
		// 前端用户输入的验证码值
		String verifycode = request.getParameter("verifycode");
		String newV = verifycode.toUpperCase();

		Subject subject = SecurityUtils.getSubject();

		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		if (newV.equals(piccode)) {

			try {
				subject.login(token);
				boolean flag = subject.isAuthenticated();
				if (flag) { // 验证通过
					// 把用户对象信息放入到session中
					Users user = (Users) subject.getPrincipal();
					subject.getSession().setAttribute("user", user);
					
					Cookie keyCookie = new Cookie("username", user.getUsername());
					keyCookie.setMaxAge(60*60*60);
//					CookieUtils.addCookie(response, keyCookie);
					return "pages/user/success.jsp";
				} else {
					
					request.setAttribute("msg", "用户或密码错误");
					return "index.jsp";
				}
			} catch (Exception e) {
				request.setAttribute("msg", "用户或密码错误");
				e.printStackTrace();
				return "index.jsp";
			}
		}else
		
		
		request.setAttribute("msg", "请输入正确验证码");
		return "index.jsp";

		// //校验密码
		// HttpSession session = request.getSession();
		// // Session中存好的验证码数值
		// String piccode = (String)
		// request.getSession().getAttribute("piccode");
		// // 前端用户输入的验证码值
		// String verifycode = request.getParameter("verifycode");
		// String newV = verifycode.toUpperCase();
		// String p1 = md5Utils.md5(password);
		// if(username!=null && !"".equals(username) && password != null &&
		// !"".equals(password) && newV.equals(piccode)){
		// Users user = us.login(username, p1);
		// if(user !=null && user.getPassword().equals(p1)){
		// //登陆成功
		// session.setAttribute("user", user);
		// return "pages/user/success.jsp";
		// }
		//
		// }else{
		// request.setAttribute("msg", "用户或密码或验证码错误");
		// return "index.jsp";
		// }
		// request.setAttribute("msg", "用户或密码或验证码错误");
		// return "index.jsp";
	}

	@RequestMapping("index")
	public String index() {
		return "index.jsp";
	}

	@RequestMapping("nofunc")
	public String nofunc() {
		return "pages/error/nofunc.jsp";
	}
	

	@RequestMapping("home")
	public String userHome() {
		return "pages/user/success.jsp";
	}
	
}
